package com.example.alpha.campusbase;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 用户数据操作集
 */
public class UserLocalStore {
    public static final String SP_NAME="UserDetails";
    SharedPreferences userLocalDatabase;
    private static final String LOGGENED="loggenIn";
    private static final String NEWLOGIN="isNewLogin";

    //提取sp
    public UserLocalStore(Context context){
        userLocalDatabase=context.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);
    }

    //保存当前用户数据
    public void storeUserData(User user){
        SharedPreferences.Editor speditor=userLocalDatabase.edit();
        speditor.putString("email",user.email);
        speditor.putString("password",user.passWord);
        speditor.putString("campusname",user.campusname);
        speditor.putString("name",user.name);
        speditor.apply();
    }

    //获取当前登录用户
    public User getLoggenUser(){
        String name=userLocalDatabase.getString("name","");
        String email=userLocalDatabase.getString("email","");
        String campusname=userLocalDatabase.getString("campusname","");
        String password=userLocalDatabase.getString("password","");
        return new User(email,password,campusname,name);
    }

    //设置用户登录状态
    public void setUserLoggenIn(Boolean loggenIn){
        SharedPreferences.Editor editor=userLocalDatabase.edit();
        editor.putBoolean(LOGGENED,loggenIn);
        editor.apply();
    }

    //获取用户登录状态
    public boolean getUserLoginState(){
        return userLocalDatabase.getBoolean(LOGGENED,false);
    }

    //清除用户数据
    public void clearUserData(){
        SharedPreferences.Editor editor=userLocalDatabase.edit();
        editor.clear();
        editor.apply();
    }

    //存储
    public void setNewlogin(Boolean newlogin){
        SharedPreferences.Editor editor=userLocalDatabase.edit();
        editor.putBoolean(NEWLOGIN,true);
        editor.apply();
    }
}
